<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Laravel\Scout\Searchable;

/**
 * Class Product
 * 商品表
 *
 * @package App\Models
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Product newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Product newQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Product query()
 * @mixin \Eloquent
 * @property int $id
 * @property int $category_id 商品的分类id
 * @property int $store_id 店铺的id
 * @property string $title 商品名称
 * @property string $introduce 商品的简介
 * @property string $description 商品的详情
 * @property string $image 展示图片
 * @property int $on_sale 是否售卖
 * @property int $sold_count 商品的销量
 * @property int $review_count 评论的数量
 * @property float $price 商品的最低价格
 * @property \Illuminate\Support\Carbon|null $created_at
 * @property \Illuminate\Support\Carbon|null $updated_at
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Product whereCategoryId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Product whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Product whereDescription($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Product whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Product whereImage($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Product whereIntroduce($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Product whereOnSale($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Product wherePrice($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Product whereReviewCount($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Product whereSoldCount($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Product whereStoreId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Product whereTitle($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Product whereUpdatedAt($value)
 * @property-read \App\Models\ProductCategory $category
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\ProductSku[] $skus
 * @property-read int|null $skus_count
 */
class Product extends Model
{
    use Searchable;
    protected $fillable=['category_id','title','introduce','description','image','on_sale','store_id','price'];

    public function searchableAs()
    {
        return 'products';
    }

    public function toSearchableArray()
    {
        $array =[
            'title'=>$this->title,
            'introduce'=>$this->introduce
        ];
        return $array;
    }
    public function skus()
    {
        return $this->hasMany(ProductSku::class);
    }

    public  function  category() {
        return $this->belongsTo(ProductCategory::class,'category_id');
    }

    /**
     * 返回搜索的数据
     * @param $content
     */
    public function searchData($content)
    {
        $search=$this->search($content)->get();
        $category=ProductCategory::find($search->pluck('category_id'));
        return ['product'=>$search,'cateory'=>$category];
    }
    /**
     * 获取当前商品所属的分类
     * @return array
     */
    public  function  getCategoryInfo() {
        $collection=$this->category->load('superior.superior');
        ////将商品的名字添加到集合中
        return $collection->pluck('name','level')->push($this->title)->toArray();
    }

    public function getImageAttribute($value)
    {
        return config('filesystems.disks.qiniu.domain').$value;
    }
}
